/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import junit.framework.TestCase;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Shell;
import org.leeloo.core.Tag;
import org.leeloo.core.TagService;
import org.leeloo.databinding.ui.TagFilterManager;

public class ManageFilterActionTest extends TestCase {
    private Shell shell;
    private TableViewer tableViewer;
    private TagService tagService;
    private TagFilterManager tagFilterManager;
    private MyManageFilterAction action;
    private Tag[] tags;
    
    protected void setUp() throws Exception {
        shell = new Shell();
        tableViewer = new TableViewer(shell);
        tagService = new TagService();
        tagFilterManager = new TagFilterManager(tableViewer, tagService);
        action = new MyManageFilterAction(tableViewer, tagFilterManager);
        tags = new Tag[] {new Tag("1", "1"), new Tag("2", "2")};
    }
    
    protected void tearDown() throws Exception {
        if (shell != null && !shell.isDisposed()) {
            shell.dispose();
        }
    }
    
    public void testOpensDialog() throws Exception {        
        action.returnCode = IDialogConstants.OK_ID;
        
        assertNull(action.dialog);
        action.run();
        assertNotNull(action.dialog);
        assertEquals(1, action.dialog.openCount);
    }
    
    public void testSetsTagFilterManagerTags() throws Exception {
        action.tags = tags;
        
        assertEquals(0, tagFilterManager.getTags().length);
        action.run();
        assertEquals(2, tagFilterManager.getTags().length);
    }
    
    public void testSetsSelection() throws Exception {
        action.tags = tags;
        action.selection = action.tags;
        
        assertEquals(0, tagFilterManager.getSelection().length);
        action.run();
        assertEquals(2, tagFilterManager.getSelection().length);
    }
    
    public void testIgnoresCancel() throws Exception {
        action.tags = tags;
        
        assertEquals(0, tagFilterManager.getTags().length);
        action.returnCode = IDialogConstants.CANCEL_ID;
        action.run();
        assertEquals(0, tagFilterManager.getTags().length);
    }
    
    private class MyManageFilterAction extends ManageFilterAction {
        private MyTagSelectionDialog dialog;
        private int returnCode;
        private Tag[] tags;
        private Tag[] selection;
        
        public MyManageFilterAction(TableViewer tableViewer, TagFilterManager tagFilterManager) {
            super(tableViewer, tagFilterManager);
        }        
        
        /**
         * @category testability
         */
        protected TagSelectionDialog doCreateDialog(TableViewer tableViewer, TagFilterManager tagFilterManager) {
            dialog = new MyTagSelectionDialog(tableViewer.getControl().getShell(), tags, selection);
            dialog.setReturnCode(returnCode);
            dialog.setBlockOnOpen(false);
            
            return dialog;
        }        
    }
    
    private static class MyTagSelectionDialog extends TagSelectionDialog {
        private int openCount;
        
        public MyTagSelectionDialog(Shell parentShell, Tag[] tags, Tag[] selection) {
            super(parentShell, tags, selection);
        }
        
        /**
         * @category testability
         */
        protected void setReturnCode(int code) {
            super.setReturnCode(code);
        }        
        
        /**
         * @category testability
         */
        public int open() {
            openCount++;
            return super.open();
        }
    }
}